﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class ManufacturerGroupDao : IManufacturerGroupDao
    {
        public List<ManufacturerGroup> GetAllManufacturerGroups()
        {
            List<ManufacturerGroup> manufacturerGroups;

            using (var context = new MonkeyWarrantyEntities())
            {
                manufacturerGroups = context.ManufacturerGroups.OrderBy(x => x.Name).ToList();
            }

            return manufacturerGroups;
        }

        public ManufacturerGroup AddManufacturerGroup(ManufacturerGroup manufacturerGroup)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.ManufacturerGroups.AddObject(manufacturerGroup);
                context.SaveChanges();
            }

            return manufacturerGroup;
        }

        public void UpdateManufacturerGroup(ManufacturerGroup manufacturerGroup)
        {
            // throw new System.NotImplementedException();
            if (manufacturerGroup == null || manufacturerGroup.Id == 0) return;

            using (var context = new MonkeyWarrantyEntities())
            {
                context.ManufacturerGroups.Attach(manufacturerGroup);
                context.ObjectStateManager.ChangeObjectState(manufacturerGroup, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteManufacturerGroup(int manufacturerGroupId)
        {
            if (manufacturerGroupId <= 0) return;
            using (var context = new MonkeyWarrantyEntities())
            {
                var manufacturerGroup = context.ManufacturerGroups.First(g => g.Id == manufacturerGroupId);

                context.DeleteObject(manufacturerGroup);

                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var manufacturerGroup = context.ManufacturerGroups.OrderByDescending(x => x.Id).FirstOrDefault();
                if (manufacturerGroup != null) maxId = manufacturerGroup.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("ManufacturerGroups").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public ManufacturerGroup GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.ManufacturerGroups.FirstOrDefault(x => x.Id == id);
            }
        }
    }
}
